package cn.fff.system.controller;

import cn.fff.common.result.Result;
import cn.fff.model.system.SysRole;
import cn.fff.model.vo.AssignMenuVo;
import cn.fff.model.vo.AssignRoleVo;
import cn.fff.model.vo.MenuTreeCheckVo;
import cn.fff.model.vo.SysRoleQueryVo;
import cn.fff.system.exception.FffCustomException;
import cn.fff.system.service.SysRoleService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "角色管理接口")
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;

    // 查询所有的记录
    @ApiOperation("查询所有角色")
    @GetMapping("/findAll")
    public Result<List<SysRole>> findAllRole() {
        List<SysRole> all = sysRoleService.list();
        return Result.ok(all);
    }

    @ApiOperation("逻辑删除接口")
    @DeleteMapping("remove/{id}")
    public Result<Void> removeRole(@PathVariable("id")String id) {
        boolean success = sysRoleService.removeById(id);
        if (success) {
            return Result.ok();
        }
        return Result.fail();
    }

    @ApiOperation("条件分页查询角色")
    @GetMapping("{page}/{limit}")
    public Result<IPage<SysRole>> findPageQueryRole(@PathVariable Long page,
                                    @PathVariable Long limit,
                                    SysRoleQueryVo sysRoleQueryVo) {
        Page<SysRole> pageParam = new Page<>(page, limit);
        IPage<SysRole> pageResult = sysRoleService.selectPage(pageParam, sysRoleQueryVo);
        return Result.ok(pageResult);
    }

    @ApiOperation("添加角色")
    @PostMapping("save")
    @ApiOperationSupport(includeParameters = {"sysRole.roleName", "sysRole.roleCode", "sysRole.description"})
    public Result<Void> save(@RequestBody SysRole sysRole) {
        boolean success = sysRoleService.save(sysRole);
        if (success) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation("根据id查询角色")
    @PostMapping("findRoleById/{id}")
    public Result<SysRole> findRoleById(@PathVariable("id") Long id) {
        SysRole sysRole = sysRoleService.getById(id);
        // Integer a = 1/0;
        if(true) throw new FffCustomException(602, "自定义的异常。");
        return Result.ok(sysRole);
    }

    @ApiOperation("修改角色")
    @PostMapping("update")
    @ApiOperationSupport(includeParameters = {"sysRole.id",
            "sysRole.roleName", "sysRole.roleCode", "sysRole.description"})
    public Result<Void> update(@RequestBody SysRole sysRole) {
        boolean success = sysRoleService.updateById(sysRole);
        if (success) return Result.ok();
        else return Result.fail();
    }

    /**
     * json 中的数组格式对应 java 中的 List 集合
     * @param ids
     * @return
     */
    @ApiOperation("批量删除角色")
    @DeleteMapping("batchRemove")
    public Result<Void> batchRemove(@RequestBody List<Long> ids) {
        boolean success = sysRoleService.removeByIds(ids);
        if (success) return Result.ok();
        else return Result.fail();
    }

    @ApiOperation("获取用户的角色数据")
    @GetMapping("toAssign/{userId}")
    public Result<?> toAssign(@PathVariable String userId) {
        return Result.ok(sysRoleService.getRolesByUserId(userId));
    }

    @ApiOperation("为用户分配角色")
    @PostMapping("doAssign")
    public Result<Void> doAssign(@RequestBody AssignRoleVo vo) {
        sysRoleService.doAssign(vo);
        return Result.ok();
    }

//    @ApiOperation("根据角色获取菜单")
//    @GetMapping("queryMenu/{roleId}")
//    public Result<List<SysMenu>> queryMenu(@PathVariable Long roleId) {
//        return Result.ok(sysRoleService.findMenuByRoleId(roleId));
//    }

    @ApiOperation("根据角色获取菜单")
    @GetMapping("queryMenu/{roleId}")
    public Result<MenuTreeCheckVo> queryMenu(@PathVariable Long roleId) {
        MenuTreeCheckVo data = sysRoleService.findMenuByRoleId(roleId);
        return Result.ok(data);
    }

    @ApiOperation("为角色分配菜单")
    @PostMapping("authMenu")
    public Result<Void> authMenu(@RequestBody AssignMenuVo vo) {
        sysRoleService.authMenu(vo);
        return Result.ok();
    }

}
